package cn.bdqn.controller;

import cn.bdqn.domain.BaseRegister;
import cn.bdqn.service.BaseRegisterService;
import cn.bdqn.utils.result.Page;
import cn.bdqn.utils.result.Result;
import cn.bdqn.utils.result.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 基地注册控制层
 * @author dkj
 */
@RestController
@RequestMapping("/BaseRegister")
public class BaseRegisterController {

    /**
     * 基地注册业务层对象
     */
    @Autowired
    private BaseRegisterService baseRegisterService;

    /**
     * 分页查询
     * @param pageNum 当前页
     * @return
     */
    @RequestMapping("/queryPage")
    public Result queryPage(@RequestParam(defaultValue = "1") Integer pageNum)throws Exception{
        try {
            //获取封装对象
            Page page = baseRegisterService.queryPage(pageNum);
            //返回结果 page.getMenuList() 分页后的数据  page.getTotalRecord()总数
            return ResultGenerator.genSuccessListResult(page.getMenuList(),page.getTotalRecord());
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 添加一个基地账号
     * @param baseRegister
     * @return
     */
    @RequestMapping("/save")
    public Result save(BaseRegister baseRegister)throws Exception{
        try {
            //调用添加方法
            baseRegisterService.save(baseRegister);
            //返回成功结果
            return ResultGenerator.genSuccessResult();
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }


    /**
     * 登录
     * @param phone
     * @param pwd
     * @return
     */
    @RequestMapping("/login")
    public Result login(String phone, String pwd)throws Exception{
        try {
            //登录根据手机号和密码查询
            BaseRegister baseRegister = baseRegisterService.queryByPhoneAndPwd(phone,pwd);

            if (baseRegister!=null){
                //返回结果
                return ResultGenerator.genSuccessResult(baseRegister);
            }
            return ResultGenerator.genFailResult("账号或密码错误");

        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 根据手机号查询基地账号是否存在
     * @param phone
     * @return
     */
    @RequestMapping("/queryByPhone")
    public Result queryByPhone(String phone)throws Exception{
        try {
            Integer count = baseRegisterService.queryByPhone(phone);
            //返回结果，如果count>0代表该手机号已经被注册了 return false
            return ResultGenerator.genSuccessResult(count > 0 ? false : true);
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }


    /**
     * 根据邮箱查询基地账号是否存在
     * @param email
     * @return
     */
    @RequestMapping("/queryByEmail")
    public Result queryByEmail(String email)throws Exception{
        try {
            Integer count = baseRegisterService.queryByEmail(email);
            //返回结果，如果count>0代表该手机号已经被注册了 return false
            return ResultGenerator.genSuccessResult(count > 0 ? false : true);
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }


    /**
     * 验证码
     * @param phone
     * @return
     */
    @RequestMapping("/verification")
    public Result verification(String phone)throws Exception{
        try {
            //调用短信验证的方法获得去验证码
            String captcha = baseRegisterService.verification(phone);
            //返回结果
            return ResultGenerator.genSuccessResult(captcha);
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 邮箱验证码
     * @param email
     * @return
     */
    @RequestMapping(value = "/emailVerification")
    public Result emailVerification(String email)throws Exception{
        try {
            //调用邮箱验证的方法获得去验证码
            String code = baseRegisterService.emailVerification(email);
           /* if(code!=null && code!=""){
                session.setAttribute("vcode", code);
                //创建TimerTask用来三分钟之后移出vcode
                TimerTask task = new TimerTask(){

                    @Override
                    public void run() {
                        session.removeAttribute("vcode");
                    }
                };

              *//*  Timer timer = new Timer();
                timer.schedule(task,180000);*//*

                //延迟3分钟后，执行且只执行一次task
                *//*ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);
                pool.schedule(task, 180000, TimeUnit.SECONDS);*//*

            }*/
            System.out.println(code);
            //返回结果
            return ResultGenerator.genSuccessResult(code);
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 更新基地账号对象
     * 根据手机号修改密码
     * @param baseRegister
     * @return
     */
    @RequestMapping("/updateById")
    public Result updateById(BaseRegister baseRegister)throws Exception{
        try {
            //更新并返回更新后的对象数据
            BaseRegister baseRegister1 = baseRegisterService.updateById(baseRegister);
            //返回对象
            return ResultGenerator.genSuccessResult(baseRegister1);
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }


    /**
     * 根据旧密码和id修改基地账号数据
     * @param id
     * @param oldPwd
     * @param newPwd
     * @return
     */
    @RequestMapping("/updateByIdAndOldPwd")
    public Result updateByIdAndOldPwd(Integer id, String oldPwd, String newPwd) throws Exception {
        try {
            BaseRegister baseRegister = baseRegisterService.updateByIdAndOldPwd(id,oldPwd,newPwd);
            //判断对象是否为空 为空代表修改失败
            if (baseRegister==null){
                return ResultGenerator.genFailResult("旧密码输入错误");
            }else {
                //修改成功,返回最新的数据对象
                return ResultGenerator.genSuccessResult(baseRegister);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }


    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @RequestMapping("/queryByPrimaryKey")
    public Result queryByPrimaryKey(Integer id)throws Exception{

        try {
            //根据id查询
            BaseRegister baseRegister = baseRegisterService.queryByPrimaryKey(id);
            return ResultGenerator.genSuccessResult(baseRegister);
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 更新基地账号
     * @param baseRegister
     * @return
     */
    @RequestMapping("/updateBaseUser")
    public Result updateBaseUser(BaseRegister baseRegister)throws Exception{
        try {
            //更新账号
            BaseRegister baseRegister1 = baseRegisterService.updateBaseUser(baseRegister);
            //返回结果
            return ResultGenerator.genSuccessResult(baseRegister1);
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }
}
